package session

import (
	"chainmaker.org/chainmaker/smarteditor/container"
	"chainmaker.org/chainmaker/smarteditor/util"
	"github.com/88250/gulu"
	"net/http"
)

// contractHasBuildReqFormData has build contr request data
type contractHasBuildReqFormData struct {
	ContractNames []string `json:"contractNames"`
}

type contractHasBuildResponseData struct {
	HasBuild bool `json:"hasBuild"`
}

//ContractHasBuildHandler return contract has build or not
func ContractHasBuildHandler(w http.ResponseWriter, r *http.Request) {

	uid, err := GetUidFromSession(r)
	if err != nil {
		http.Error(w, "Forbidden", http.StatusForbidden)
		return
	}

	result := gulu.Ret.NewResult()
	defer gulu.Ret.RetResult(w, r, result)

	requestData := new(contractHasBuildReqFormData)
	err = util.ReadArgsTo(r.Body, requestData)
	if err != nil {
		result.Code = -1
		log.Errorf("ContractHasBuildHandler unmarshal parameter fail. err:%s", err)
		return
	}

	result.Data = &contractHasBuildResponseData{
		HasBuild: container.SC.BuildContractCache.HasBuild(uid, requestData.ContractNames...),
	}
}
